/*
 * 兴趣标签
 * @Author: along
 * @Date: 2019-12-24 10:04:00
 */
<template>
    <div class="container studentCode">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left"/>
            <div class="header-right">
                <div class="header-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="fnCreateCode()"
                    >新建</el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="studentCode"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="invite_code"
                label="临时邀请码"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="valid_second"
                label="邀请码时效"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <span v-if="scope.row.valid_second > 0">{{ scope.row.valid_second | setHous }}</span>
                    <span v-else>--</span>
                </template>
            </el-table-column>
            <el-table-column
                label="创建时间"
                prop="create_time"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <span>{{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="remark"
                label="备注"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                label="操作"
                align="center"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fneditCode(scope.row)"
                    >
                        编辑
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnDeleteCode(scope.row)"
                    >
                        删除
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnRemarks(scope.row)"
                    >
                        备注
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- 临时邀请码 -->
        <el-dialog
            title="临时邀请码"
            :visible.sync="dialogVisible"
            width="500px"
        >
            <el-form
                :model="ruleForm"
                :rules="rules"
                ref="ruleForm"
                label-width="100px"
                class="demo-ruleForm"
            >
                <el-form-item
                    label="临时邀请码"
                    prop="code"
                >
                    <div style="display: flex">
                        <el-input
                            v-model="ruleForm.code"
                            size="small"
                            maxlength="6"
                            placeholder="请输入临时邀请码"
                        />
                        <span style="white-space: nowrap; color:#606266;marginLeft: 10px">限6位数字</span>
                    </div>
                </el-form-item>
                <el-form-item
                    label="时间限制"
                    prop="time"
                >
                    <div style="display: flex">
                        <el-input
                            v-model="ruleForm.time"
                            size="small"
                            placeholder="请输入时间"
                            type="number"
                            min="0"
                        />
                        <span style="white-space: nowrap; color:#606266;marginLeft: 10px;marginRight:37px">小时</span>
                    </div>
                </el-form-item>
            </el-form>
            <span
                slot="footer"
                class="dialog-footer">
                <el-button
                    @click="dialogVisible = false"
                    size="small"
                >取 消</el-button>
                <el-button
                    type="primary"
                    size="small"
                    @click="fnConfrimCode('ruleForm')">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 临时邀请码 -->

        <!-- 备注内容 -->
        <el-dialog
            title="备注内容"
            width="530px"
            custom-class="schedule-dialog"
            :visible.sync="showMask"
        >
            <el-form
                :model="ruleFormMask"
                :rules="rulesMask"
                ref="ruleFormMask"
            >
                <el-form-item
                    prop='reportCont'
                >
                    <el-input
                        type="textarea"
                        :autosize="{ minRows: 2, maxRows: 4}"
                        placeholder="请输入内容"
                        v-model="ruleFormMask.reportCont"
                    />
                </el-form-item>
            </el-form>
            <div
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    size="small"
                    type="primary"
                    @click="submitForm('ruleFormMask')">提交</el-button>
            </div>
        </el-dialog>
        <!-- 备注内容 -->

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>
    </div>
</template>

<script>
import { back } from '@/mixin/back';
export default {
    /**
     * @param  {Object}    authResult           //表单权限
     * @param  {Array}     tableData            //表单数据
     * @param  {Object}    page                 //表单分页
     */
    mixins: [back],
    data () {
        return {
            ruleFormMask: {
                reportCont: ''
            },
            rulesMask: {
                reportCont: [
                    { required: true, message: '请填写备注内容', trigger: 'blur' }
                ]
            },
            showMask: false,
            temp_no: '',
            typeModel: '',
            dialogVisible: false,
            rules: {
                code : [
                    { required: true, message: '请填写临时邀请码,不可以为空', trigger: 'blur' }
                ],
                time: [
                    { required: true, message: '请填写时间限制,不可以为空', trigger: 'blur' }
                ]
            },
            ruleForm: {
                code: '',
                time: ''
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            pathName: '',
            tableData: [],
            authResult: {}
        };
    },
    filters: {
        setHous (val) {
            return (val / 60 / 60).toFixed(1) + '小时';
        }
    },
    computed: {},
    watch: {
        showMask (val) {
            if(!val) {
                this.ruleFormMask.reportCont = '';
                this.$refs['ruleFormMask'].resetFields();
            }
        }
    },
    mounted () {
        this.authResult = this.$route.meta.authResult;
        this.fnBreadcrumb();
        this.getTableData();
    },
    methods: {
        /**
         * 设置表单header
         */
        fnBreadcrumb () {
            this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '掌大精英'},{title: '临时邀请码'}]);
        },

        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/stuagent_temp_invite_code%5Cget_list', {
                page_id: page || this.page.now_page
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 新建临时邀请码
         */
        fnCreateCode () {
            this.typeModel = 'add';
            this.ruleForm = {
                code: '',
                time: ''
            };
            this.dialogVisible = !this.dialogVisible;
        },

        /**
         * 编辑
         */
        fneditCode (row) {
            this.typeModel = 'edit';
            this.temp_no = row.temp_no;
            this.ruleForm = {
                code: row.invite_code,
                time: (row.valid_second / 60 / 60).toFixed(1)
            };

            this.dialogVisible = !this.dialogVisible;
        },

        /**
         * 新增/编辑邀请码
         */
        fnConfrimCode (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.typeModel == 'add') {
                        this.$post('/student/stuagent_temp_invite_code%5Cadd',{
                            invite_code: this.ruleForm.code,
                            valid_second: this.ruleForm.time * 60 * 60
                        }, resp=>{
                            if (resp.code == 1) {
                                this.getTableData();
                                this.dialogVisible = !this.dialogVisible;
                                this.$message({
                                    type: 'success',
                                    message: '新建成功',
                                    center: true
                                });
                            } else {
                                this.$message({
                                    type: 'waring',
                                    message: resp.msg,
                                    center: true
                                });
                            }
                        });
                    } else if (this.typeModel == 'edit') {
                        this.$post('/student/stuagent_temp_invite_code%5Cset',{
                            temp_no: this.temp_no,
                            invite_code: this.ruleForm.code,
                            valid_second: this.ruleForm.time * 60 * 60
                        }, resp=>{
                            if (resp.code == 1) {
                                this.getTableData();
                                this.dialogVisible = !this.dialogVisible;
                                this.$message({
                                    type: 'success',
                                    message: '编辑成功',
                                    center: true
                                });
                            } else {
                                this.$message({
                                    type: 'waring',
                                    message: resp.msg,
                                    center: true
                                });
                            }
                        });
                    } else {
                        return;
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        /**
         * 删除临时邀请码
         */
        fnDeleteCode (row) {
            this.$confirm('是否要删除该临时邀请码？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/stuagent_temp_invite_code%5Cdelete',{
                    temp_no: row.temp_no
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '删除成功',
                            center: true
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'warning',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            });
        },

        /**
         * 备注
         */
        fnRemarks (row) {
            this.showMask = !this.showMask;
            this.temp_no = row.temp_no;
        },

        /**
         * 提交备注
         */
        submitForm (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$post('/student/stuagent_temp_invite_code%5Cset_remark',{
                        temp_no: this.temp_no,
                        remark: this.ruleFormMask.reportCont
                    },resp=>{
                        if (resp.code == 1) {
                            this.getTableData();
                            this.showMask = !this.showMask;
                            this.$message({
                                type: 'success',
                                message: '提交成功',
                                center: true
                            });
                        } else if (resp.code == 0) {
                            this.$message({
                                type: 'warning',
                                message: resp.msg,
                                center: true
                            });
                        }
                    });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        /**
         * 获取路由参数
         */
        fnParamUrl () {
            let query = this.$route.query;
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
            }
            .header-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
</style>
<style lang="less">
    .studentCode .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .studentCode .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
    .studentCode .el-table .el-table_1_column_6  .cell {
        position: relative;
    }
    .studentCode .el-range-editor--small .el-range-separator{
        padding: 0 1px !important;
    }
</style>